import { Component, OnInit, Injector, ViewChild } from '@angular/core';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { AppComponentBase } from '@shared/common/app-component-base';
import { Table } from 'primeng/table';
import { Paginator, LazyLoadEvent } from 'primeng/primeng';
import { PrimengTableHelper } from '@shared/helpers/PrimengTableHelper';
import {
    StockZT_OutBillItemServiceProxy,
    ListDtoExtensionsItem,
    Stock_BillListDto,
    Stock_OutToolingItemListDto,
    StockZT_AuditServiceProxy
} from '@shared/service-proxies/stock-service-proxies';
import * as _ from 'lodash';
import { element } from '@angular/core/src/render3';
import { finalize } from 'rxjs/operators';
import { UserDataPermissionServiceProxy } from '@shared/service-proxies/stock-service-proxies';
import { CreateToolingReceiveOpinionModalComponent } from './create-or-edit-tooling-receive-opinion-modal.component';
import * as moment from 'moment';

@Component({
    templateUrl: './tooling-search-receive.component.html',
    animations: [appModuleAnimation()],
    selector: 'marketout'
})
export class ToolingSearchReceiveComponent extends AppComponentBase {
    @ViewChild('dataTable') dataTable: Table;
    @ViewChild('paginator') paginator: Paginator;

    @ViewChild('detailDataTable') detailDataTable: Table;
    @ViewChild('detailPaginator') detailPaginator: Paginator;
    @ViewChild('createToolingReceiveOpinionModalComponent') createOpinionModalComponent: CreateToolingReceiveOpinionModalComponent;
    // filters
    warehouseNameFilter = '';
    billcode = '';
    totalPrice = '0';
    f_MaterialCode = '';
    f_MaterialName = '';
    f_TradeMark = '';
    f_Specification = '';
    f_WarehouseName = '';
    f_BillCode = '';
    f_Operator = '';
    roomOption;
    selectedWareHouse;
    advancedFiltersAreShown = false;
    PlanNumber= '';
    ContractNumber= '';
    public dateRange: Date[] = [
        moment()
            .startOf('day')
            .add(-1, 'month')
            .toDate(),
        moment()
            .endOf('day')
            .toDate()
    ];

    userDataPermissions;
    selectedDepts;
    extensions: Array<ListDtoExtensionsItem>;
    detialExtensions: Array<ListDtoExtensionsItem>;

    detailPrimengTableHelper: PrimengTableHelper;
    selectedBill: Stock_BillListDto;

    billstatus = '';
    f_isCurrentUser;
    selectedBillItem = new Stock_OutToolingItemListDto();

    constructor(
        injector: Injector,
        private _billService: StockZT_OutBillItemServiceProxy,
        private _userDataPermissionServiceProxy: UserDataPermissionServiceProxy,
        private _auditService: StockZT_AuditServiceProxy
    ) {
        super(injector);
        this.detailPrimengTableHelper = new PrimengTableHelper();
    }

    ngOnInit(): void {
        this._userDataPermissionServiceProxy.getUserDataPermissions('Base_Bill').subscribe(result => {
            this.userDataPermissions = result.items.map(e => {
                return {
                    label: e.displayText,
                    value: e
                };
            });
            this._billService
            .getWarehouseCombobox('工装库')
            .subscribe(result2 => {
                this.roomOption = result2.items.map(e => {
                    return {
                        label: e.displayText,
                        value: e
                    };
                });

                this.selectedWareHouse = result2.items;
                this.getBillItems();
            });
            
            
        });
    }
   
    
    reloadData(event) {
        if (event == null || event == undefined) {
            this.getBillItems();
        } else {
            this.getBillItems();
        }
    }
    getBillItems(event?: LazyLoadEvent) {
        if (this.detailPrimengTableHelper.shouldResetPaging(event)) {
            this.detailPaginator.changePage(0);

            return;
        }

        this.primengTableHelper.showLoadingIndicator();

        this._billService
            .getOutToolingItemListForSearchExtension(
                this.PlanNumber,
                this.ContractNumber,
                this.f_BillCode,
                this.f_Operator,
                this.f_MaterialCode,
                this.f_MaterialName,
                this.f_TradeMark,
                this.f_Specification,
                this.warehouseNameFilter,
                moment(this.dateRange[0]),
                moment(this.dateRange[1]),
                this.detailPrimengTableHelper.getSorting(this.detailDataTable),
                this.detailPrimengTableHelper.getMaxResultCount(this.detailPaginator, event),
                this.detailPrimengTableHelper.getSkipCount(this.detailPaginator, event)
            )
            .subscribe(result => {
                this.detialExtensions = result.items;
                this.detailPrimengTableHelper.totalRecordsCount = result.dtos.totalCount;
                this.detailPrimengTableHelper.records = result.dtos.items;
                this.totalPrice = result.data;
                this.primengTableHelper.hideLoadingIndicator();               
            });
    }
}
